package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.*;
import com.example.demo.entity.*;
import com.example.demo.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ShowPageController {
    @Autowired
    private CourseMapper cm;
    @Autowired
    private ScoreMapper sm;
    @Autowired
    private  WarningInfoMapper wm;
    @Autowired
    private  StudentMapper smm;
    @Autowired
    private TeacherMapper tm;
    @Autowired
    private Score_CopyMapper scm;
    @RequestMapping("/showpage")
    public void showPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String spage=request.getParameter("pageNum");
        String types=request.getParameter("types");
        int uploadid=0;
        try {
            uploadid = Integer.valueOf(request.getParameter("uploadid"));
        }catch (Exception e){
            System.err.println(e.getMessage());
        }
       int num=1;
       try {
           num = Integer.valueOf(request.getParameter("pageSize"));
       }catch (Exception e){
           num=10;
       }
       int page=1;
       try {
           page=Integer.valueOf(spage);
       }catch (Exception e){
           page=1;
       }
       if("course".equals(types)) {
           List<Course> list = new ArrayList<>();
           list = cm.findAlllCourse();
           List<Object> objectList = new ArrayList<>(list);
           List<Object> newlist = PageUtil.getPageData(objectList, page, num);
           JSONObject result = new JSONObject();
           result.put("newlist", newlist);
           result.put("total", list.size());
           response.getWriter().write(JSONObject.toJSONString(Message.success(result)));
       }else if("score".equals(types)){
           List<Score> list=new ArrayList<>();
           list=sm.findAllScore();
           List<Object> objectList = new ArrayList<>(list);
           List<Object> newlist = PageUtil.getPageData(objectList, page, num);
           JSONObject result = new JSONObject();
           result.put("newlist", newlist);
           result.put("total", list.size());
           response.getWriter().write(JSONObject.toJSONString(Message.success(result)));
       }else  if("stu".equals(types)){
           List<Student> list=new ArrayList<>();
           list=smm.findAlllStu();
           List<Object> objectList = new ArrayList<>(list);
           List<Object> newlist = PageUtil.getPageData(objectList, page, num);
           JSONObject result = new JSONObject();
           result.put("newlist", newlist);
           result.put("total", list.size());
           response.getWriter().write(JSONObject.toJSONString(Message.success(result)));
       }else  if("tea".equals(types)){
           List<Teacher> list=new ArrayList<>();
           list=tm.findAllTea();
           List<Object> objectList = new ArrayList<>(list);
           List<Object> newlist = PageUtil.getPageData(objectList, page, num);
           JSONObject result = new JSONObject();
           result.put("newlist", newlist);
           result.put("total", list.size());
           response.getWriter().write(JSONObject.toJSONString(Message.success(result)));
       }else if("score_copy".equals(types)){
           List<Score_copy> list=new ArrayList<>();
           list=scm.findScoreByUploadId(uploadid);
           List<Object> objectList = new ArrayList<>(list);
           List<Object> newlist = PageUtil.getPageData(objectList, page, num);
           JSONObject result = new JSONObject();
           result.put("newlist", newlist);
           result.put("total", list.size());
           response.getWriter().write(JSONObject.toJSONString(Message.success(result)));
       } else {
           List<WarningInfos> list=new ArrayList<>();
           list=wm.selectAll();
           List<Object> objectList = new ArrayList<>(list);
           List<Object> newlist = PageUtil.getPageData(objectList, page, num);
           List<Object> ss = new ArrayList<>(list);
           JSONObject result = new JSONObject();
           result.put("newlist", newlist);
           result.put("total", list.size());
           response.getWriter().write(JSONObject.toJSONString(Message.success(result)));
       }
    }
}
